package cn.huihuo.weibo.utils;

import java.net.URL;

/**
 * @author Dan
 * Created by Administrator on 2015/4/20.
 */
public class URLToParamUtil {
    /**
     * 返回好解析的函数
     * @param url
     * @return
     */
    public static URLParam toParam(URL url){
        URLParam urlParam = new URLParam();


        findQueryParam(urlParam, url);

        findPathParam(urlParam,url);

        urlParam.setAuthority(url.getAuthority());



        return urlParam;
    }

    private static void findPathParam(URLParam urlParam, URL url) {

        String pathStr = url.getPath();
        if(pathStr==null){
            return;
        }
        String[] pathes = pathStr.split("/");
        for(String path:pathes){
            if(!(path==null||path.equals(""))){
                urlParam.getPathParam().add(path);
            }
        }
    }

    private static void findQueryParam(URLParam urlParam, URL url) {
        String query = url.getQuery();
        if(query==null){
            return ;
        }
        String[] querys = query.split("&");
        for(String queryParams:querys){
            String[] split = queryParams.split("=");
            if(split.length==2){
                urlParam.getQueryParam().put(split[0],split[1]);
            }
        }
    }
}
